package com.lianniu.web.tea.pay.controller;

import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpClientTest {
	
	 private static final String UNI_URL ="https://api.mch.weixin.qq.com/pay/unifiedorder";
	
	public static void main(String[] args) throws Exception {
		  //test1();
		  //test2();
			test3();
	}
	
	public static void test2() throws Exception{
		String openid = "o6Tu-1AzIgLRomhl9MKsxuuCH-Bg";
		//String accessToken = "6_PHKJ8fNDM05gpVLqHF04KpLqyBN1TFSXYMXLLnITbr3pdBG-URiC5r0ducP-xuRzRgu_TCeegl-QSl58PMtTs5NnatPYHfNehyP7UQ84tedESiJOxBBxvM82GhY85kWjtdEEuBGxQ95bRWBxMTNdACASZT";
		String accessToken = "6_jAYS8fSKBC908Su48llUNT0LLF2vZLyUZZH8adA-35GIcfj_qvZwOiO3NGuucO4foicP3fpgWxYmlQz5GY75qg";
		//String accessToken = "6_m6juKewhYR1deGOu2-6O2DC9zhr_EiwyHgk0hakqGK2Q2wQGi9j89o5jqpJ40_mqbNDR7iWEJ8bhEg862yz5Bdo-";
		
		String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
		url = url.replace("OPENID", openid);
		url = url.replace("ACCESS_TOKEN", accessToken);
		//创建默认的httpClient实例.   
        CloseableHttpClient httpclient = null;  
        //接收响应结果  
        CloseableHttpResponse response = null;  
        try {  
            //创建httppost  
            httpclient = HttpClients.createDefault();    
            HttpGet httpGet = new HttpGet(url);
            response = httpclient.execute(httpGet);  
            //解析返结果  
            HttpEntity entity = response.getEntity();   
            if(entity != null){  
                String resStr = EntityUtils.toString(entity, "UTF-8");      
                System.out.println(resStr);  
            }  
        }catch (Exception e) {
        		e.printStackTrace();
		}
	}
	
	public static void test3() throws Exception{
		String appId = "wx6381255700ad101d";
		String accessToken = "6_fIUs0nLoZtP7_cO6zVYik4arbcHelMhgYx0Bz20tRvJBWs6YIF1PEi-8a6_oiQ0liKf3FcYIsxqydnGiqKuqAg";
		
		String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
		url = url.replace("APPID", appId);
		url = url.replace("REFRESH_TOKEN", accessToken);
		//创建默认的httpClient实例.   
        CloseableHttpClient httpclient = null;  
        //接收响应结果  
        CloseableHttpResponse response = null;  
        try {  
            //创建httppost  
            httpclient = HttpClients.createDefault();    
            HttpGet httpGet = new HttpGet(url);
            response = httpclient.execute(httpGet);  
            //解析返结果  
            HttpEntity entity = response.getEntity();   
            if(entity != null){  
                String resStr = EntityUtils.toString(entity, "UTF-8");      
                System.out.println(resStr);  
            }  
        }catch (Exception e) {
        		e.printStackTrace();
		}
	}
	
	public static void test1() throws Exception{
		//创建默认的httpClient实例.   
        CloseableHttpClient httpclient = null;  
        //接收响应结果  
        CloseableHttpResponse response = null;  
        try {  
            //创建httppost  
            httpclient = HttpClients.createDefault();    
            //String url ="http://192.168.16.36:8081/goSearch/gosuncn/deleteDocs.htm";  
            HttpPost httpPost = new HttpPost(UNI_URL);  
            httpPost.addHeader(HTTP.CONTENT_TYPE,"text/xml");  
            //参数  
            //String json ="{'ids':['html1','html2']}";  
            String xml = "<xml><appid>wxb0f5fe89c5840bf4</appid><body><![CDATA[2016]]></body><mch_id>1489796672</mch_id><nonce_str>5f635ccf7bd8ab4ef0ed892e5af22890</nonce_str><notify_url>http://www.nashannacha.com:80/LianniuWeb/tea/chooseWXPay/pay.do</notify_url><openid>o6Tu-1AzIgLRomhl9MKsxuuCH-Bg</openid><out_trade_no>82c6c75e921a47b1910b4be107839173</out_trade_no><sign><![CDATA[CC27E764A216DF4DA5A8ED062291ADE7]]></sign><spbill_create_ip>123.151.77.73</spbill_create_ip><total_fee>1</total_fee><trade_type>JSAPI</trade_type></xml>";
            //StringEntity se = new StringEntity(new String(xml.getBytes("GBK"),"utf-8"));  
            StringEntity se =new StringEntity(xml, "utf-8");
            se.setContentEncoding("UTF-8");  
            se.setContentType("text/xml");//发送json需要设置contentType  
            httpPost.setEntity(se);  
            response = httpclient.execute(httpPost);  
            //解析返结果  
            HttpEntity entity = response.getEntity();   
            if(entity != null){  
                String resStr = EntityUtils.toString(entity, "UTF-8");      
                System.out.println(resStr);  
            }  
            Charset defaultCharset = Charset.defaultCharset();
    		String name = defaultCharset.name();
    		System.out.println("字符集："+name);
        } catch (Exception e) {  
            throw e;  
        }finally{  
            httpclient.close();  
            response.close();  
        }
	}
}
